-- Get variables from settings
local rewardPerStar = get("pRewardPerStar")
local jailTimePerStar = get("pJailTimePerStar")
local friendlyFire = get("pFriendlyFire")

-- Function that starts when a player gets hit
function police (attacker, weapon, bodypart, loss)
	if (attacker) then
		if getElementType (attacker) == "player" then
			local wantedLevel = getPlayerWantedLevel (source)
			local attackerTeam = getPlayerTeam (attacker)
			if (attackerTeam) then
				teamName = getTeamName (attackerTeam)
			end
			-- Check if attacker is cop and uses a police baton (nightstick)
			if (teamName == "cops") and (weapon == 3) and (wantedLevel >= 1)  then
				local jailTime = (jailTimePerStar * wantedLevel)
				local reward = (rewardPerStar * wantedLevel)
				outputChatBox ("Jail time: "..(jailTime / 1000).." seconds.", source)
				setElementInterior (source, 6, 264.16, 77.5, 1001.04)
				outputChatBox (getPlayerName (source).." has been arrested by "..getPlayerName (attacker)..".")
				setTimer (setElementInterior, jailTime, 1, source, 0, 1553.35, -1675.74, 16.20)
				setTimer (setPlayerWantedLevel, jailTime, 1, source, 0)
			end
		end
	end
end

-- Make a player a cop when he inserts /cop or go to cop marker
function becomeCop ()
	local teamCops = getTeamFromName ("cops")
	local playerTeam = getPlayerTeam (source)
	if (playerTeam) ~= (teamCops) then
		setPlayerTeam (source, teamCops)
		setElementModel (source, 280)
		giveWeapon (source, 22, 1700)
		giveWeapon (source, 3, 1, true)
		setPedAnimation (source, "CLOTHES", "CLO_Buy", -1, false)
		outputChatBox ("You are now a cop!", source)
	else
		outputChatBox ("You already are a cop.", source)
	end
end

-- Create police gate
function makePoliceGate ()
	if (policeGate == true) then
		outputDebugString ("Police gate is allready created!")
	else
		barrier = createObject ( 968, 1544.7355957031, -1623.9633789063, 12.950497627258, 0, -90, 90 )
		policeGate = true
	end
end
 
-- Open police gate
function openPoliceGate (source)
	local teamCops = getTeamFromName ("cops")
	local playerTeam = getPlayerTeam (source)
	if (playerTeam == teamCops) then
		if (policeGateOpened == true) then
			-- Nothing
		else
			playSoundFrontEnd (source, 46)
			moveObject ( barrier, 1000, 1544.7355957031, -1623.9633789063, 12.950497627258, 0, 90, 0 )
			policeGateOpened = true
			policeGateClosed = false
		end
	else
		outputChatBox ("Only cops can open/close the gate.", source)
	end
end

-- Close police gate
function closePoliceGate (source)
	local teamCops = getTeamFromName ("cops")
	local playerTeam = getPlayerTeam (source)
	if (playerTeam == teamCops) then
		if (policeGateClosed == true) then
			-- Nothing
		else
			playSoundFrontEnd (source, 46)
			moveObject ( barrier, 1000, 1544.7355957031, -1623.9633789063, 12.950497627258, 0, -90, 0 )
			policeGateClosed = true
			policeGateOpened = false
		end
	else
		outputChatBox ("Only cops can open/close the gate.", source)
	end
end

-- Gate Events
addEvent ( "openPoliceGate", true )
addEvent ( "closePoliceGate", true )
addEventHandler ( "closePoliceGate", getRootElement(), closePoliceGate )
addEventHandler ( "openPoliceGate", getRootElement(), openPoliceGate )
addEventHandler ( "onResourceStart", getRootElement(), makePoliceGate )
addCommandHandler ("openGate", openPoliceGate)
addCommandHandler ("closeGate", closePoliceGate)

addEvent("acceptCop", true)
addEventHandler("acceptCop", getRootElement(), becomeCop)
addEventHandler ("onPlayerDamage", getRootElement(), police)